package Action.user;

import Bean.BookInfoEntity;
import Bean.LoanBookLogRecord;
import Bean.Log.OrderLog;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import java.util.LinkedList;
import java.util.List;

public class MyOrderRecord extends ActionSupport {
    List<OrderLog> orderLogs;
    List<LoanBookLogRecord> loanBookLogRecords;
    List<Object[]> data=new LinkedList<>();
    String key;
    Session session;
    int usid;

    public String find_order_record(){
        try {
            usid = (int) ActionContext.getContext().getSession().get("userid");
        } catch (Exception e) {
            return LOGIN;
        }
        session= HibernateTools.getSession();
        //获取用户所有订单
        loanBookLogRecords=session.createQuery("from LoanBookLogRecord where userId=? order by loanTime desc ")
                .setParameter(0,usid)
                .getResultList();

        if (key!=null&&!key.equals(""))
        {
            filter();
        }
        //订单日志获取和前台数据构建
        build_orderLog();
        return SUCCESS;
    }

    private void build_orderLog() {
        for (LoanBookLogRecord loanBookLogRecord : loanBookLogRecords) {

            // 图书信息获取
            BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
            if (bookInfoEntity==null){
                bookInfoEntity=new BookInfoEntity();
                bookInfoEntity.setBook_name("已删除图书,原ID:"+loanBookLogRecord.getId());
                bookInfoEntity.setId(-1);
            }
            String q = "from OrderLog where orderid="+loanBookLogRecord.getId();
            orderLogs=session.createQuery(q).getResultList();
            for (OrderLog orderLog : orderLogs) {

                //日志id--订单号--时间--图书名字--日志内容--图书id
                data.add(new Object[]{orderLog.getId(),loanBookLogRecord.getId(),TimeTools.timeStamp2Date(orderLog.getTimestamp(), null),bookInfoEntity.getBook_name(),orderLog.getLogdata(),bookInfoEntity.getId()});
            }
        }

    }

    private void filter() {
        LinkedList<LoanBookLogRecord> middle=new LinkedList<>();
        for (LoanBookLogRecord loanBookLogRecord : loanBookLogRecords) {
            //筛选相关订单
            //订单号匹配
            if ((""+loanBookLogRecord.getId()).contains(key)){
                middle.add(loanBookLogRecord);
                continue;
            }
            //图书信息匹配
            BookInfoEntity bookInfoEntity=session.get(BookInfoEntity.class, loanBookLogRecord.getBookId());
            if (bookInfoEntity==null){
                continue;
            }
            //图书id模糊搜索
            if ((""+bookInfoEntity.getId()).contains(key)){
                middle.add(loanBookLogRecord);
                continue;
            }
            //图书名字模糊搜索
            if (bookInfoEntity.getBook_name().contains(key)){
                middle.add(loanBookLogRecord);
                continue;
            }//图书作者模糊搜索
            if (bookInfoEntity.getBook_Author().contains(key)){
                middle.add(loanBookLogRecord);
            }

        }
        loanBookLogRecords=middle;
    }

    public List<Object[]> getData() {
        return data;
    }

    public void setData(List<Object[]> data) {
        this.data = data;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

}
